package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @version 1.0
 * @auther gwt
 * @date 2021/6/18 22:48
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Date orderDate = orderSetting.getOrderDate();
                long count = orderSettingDao.findNumberOrderSetting(orderDate);
                if (count > 0) {
                    orderSettingDao.editOrderSetting(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {

        String begin = date + "-1";
        String end = date + "-31";
        HashMap<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            HashMap<String, Integer> resultMap = new HashMap<>();
            int orderDate = orderSetting.getOrderDate().getDate();
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            resultMap.put("date", orderDate);
            resultMap.put("number", number);
            resultMap.put("reservations", reservations);
            result.add(resultMap);
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        Date orderDate = orderSetting.getOrderDate();
        long count = orderSettingDao.findNumberOrderSetting(orderDate);
        if(count>0){
            orderSettingDao.editOrderSetting(orderSetting);
        }
        else {
            orderSettingDao.add(orderSetting);
        }
    }


}
